import unittest
import sys
sys.path.append('../')
from patterns.Singleton  import PerThreadSingleton,Singleton
class TestSingleton(unittest.TestCase):
	def testONe(self):
		a=Singleton()
		b=Singleton()
		self.assertEqual(a,b)
	def testTwo(self):
		class MySingleton(Singleton):pass
		a=MySingleton()
		b=Singleton()
		self.assertNotEqual(a,b)	
class TestPerThreadSingleton(unittest.TestCase):
	def testOne(self):
		a=PerThreadSingleton()
	
	
		b=PerThreadSingleton()
		self.assertEqual(a,b)
	
		def f(arg):
			c=PerThreadSingleton()
			d=PerThreadSingleton()
			self.assertEqual(c,d)
			self.assertNotEqual(a,c)
			self.assertNotEqual(b,c)
			
			
			self.assertNotEqual(a,d)
	
		import threading
		threading.Thread(target=f,args=(a,)).start()
		import time
		time.sleep(1)
	def testTwo(self):
		class MyPerThreadSingleton(PerThreadSingleton):pass
		a=MyPerThreadSingleton()
		b=PerThreadSingleton()
		self.assertNotEqual(a,b)
if __name__=='__main__':
	unittest.main()		
		